#!/bin/bash
function redis_alone() {
    msg="修改配置文件"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n70\n$msg\nXXX"
    echo 70
    mkdir -p $redisPath/data/$redisPort >/dev/null 2>&1
    /bin/cp -f $redisPath/conf/redis.conf $redisPath/conf/$redisPort.conf >>$logs_file 2>&1
    sed -i "s/^port.*/port $redisPort/g" $redisPath/conf/$redisPort.conf >>$logs_file 2>&1 
    sed -i "s#^pidfile.*#pidfile $redisPath/pid/redis_$redisPort.pid#g" $redisPath/conf/$redisPort.conf >>$logs_file 2>&1
    sed -i "s#^logfile.*#logfile \"$redisPath/logs/redis_$redisPort.log\"#g" $redisPath/conf/$redisPort.conf >>$logs_file 2>&1
    sed -i "s#^dir.*#dir $redisPath/data/$redisPort#g" $redisPath/conf/$redisPort.conf >>$logs_file 2>&1
    msg="启动redis单节点模式"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n80\n$msg\nXXX"
    echo 80
    if [ "$system_ver" != "6" ];then
        /bin/mv -f /etc/systemd/system/redis-$redisPort.service /etc/systemd/system/redis.service  >>$logs_file 2>&1
        systemctl daemon-reload >/dev/null 2>&1
        systemctl start redis.service >>$logs_file 2>&1
    else
        mv /etc/init.d/redis-$redisPort /etc/init.d/redis  >>$logs_file 2>&1
        /etc/init.d/redis >>$logs_file 2>&1        
    fi
}

function redis_sentinel() {
    msg="修改配置文件"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n70\n$msg\nXXX"
    echo 70
    redisPort1=`expr $redisPort + 1`
    if [ "$PortCheck" == "Yes" ];then
        PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${redisPort1} "`
        while [[ -n $PortUsed_Pro ]]
        do
          redisPort1=`expr $redisPort1 + 1`
          PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${redisPort1} "`
        done
    fi
    redisPort2=`expr $redisPort1 + 1`
    if [ "$PortCheck" == "Yes" ];then
        PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${redisPort2} "`
        while [[ -n $PortUsed_Pro ]]
        do
          redisPort2=`expr $redisPort2 + 1`
          PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${redisPort2} "`
        done
    fi
    sentinelPort=`expr $redisPort + 20000`
    if [ "$PortCheck" == "Yes" ];then
        PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${sentinelPort} "`
        while [[ -n $PortUsed_Pro ]]
        do
          sentinelPort=`expr $sentinelPort + 1`
          PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${sentinelPort} "`
        done
    fi
    sentinelPort1=`expr $redisPort1 + 20000`
    if [ "$PortCheck" == "Yes" ];then
        PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${sentinelPort1} "`
        while [[ -n $PortUsed_Pro ]]
        do
          sentinelPort1=`expr $sentinelPort1 + 1`
          PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${sentinelPort1} "`
        done
    fi
    sentinelPort2=`expr $redisPort2 + 20000`
    if [ "$PortCheck" == "Yes" ];then
        PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${sentinelPort2} "`
        while [[ -n $PortUsed_Pro ]]
        do
          sentinelPort2=`expr $sentinelPort2 + 1`
          PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${sentinelPort2} "`
        done
    fi
    
    mkdir -p $redisPath/data/{$redisPort,$redisPort1,$redisPort2,$sentinelPort,$sentinelPort1,$sentinelPort2} >/dev/null 2>&1
    #redis配置
    /bin/cp -f $redisPath/conf/redis.conf $redisPath/conf/$redisPort.conf >>$logs_file 2>&1
    sed -i "s/^port.*/port $redisPort/g" $redisPath/conf/$redisPort.conf >>$logs_file 2>&1
    sed -i "s#^pidfile.*#pidfile $redisPath/pid/redis_$redisPort.pid#g" $redisPath/conf/$redisPort.conf >>$logs_file 2>&1
    sed -i "s#^logfile.*#logfile \"$redisPath/logs/redis_$redisPort.log\"#g" $redisPath/conf/$redisPort.conf >>$logs_file 2>&1
    sed -i "s#^dir.*#dir $redisPath/data/$redisPort#g" $redisPath/conf/$redisPort.conf >>$logs_file 2>&1
    
    /bin/cp -f $redisPath/conf/$redisPort.conf $redisPath/conf/$redisPort1.conf >>$logs_file 2>&1
    sed -i "s/$redisPort/$redisPort1/g" $redisPath/conf/$redisPort1.conf >>$logs_file 2>&1
    sed -i "/slaveof <masterip>/a slaveof $ipaddr $redisPort" $redisPath/conf/$redisPort1.conf >>$logs_file 2>&1
    
    /bin/cp -f $redisPath/conf/$redisPort.conf $redisPath/conf/$redisPort2.conf >>$logs_file 2>&1
    sed -i "s/$redisPort/$redisPort2/g" $redisPath/conf/$redisPort2.conf >>$logs_file 2>&1
    sed -i "/slaveof <masterip>/a slaveof $ipaddr $redisPort" $redisPath/conf/$redisPort2.conf >>$logs_file 2>&1

    #哨兵配置
    sed -i "s/^sentinel monitor mymaster.*/sentinel monitor mymaster $ipaddr 6379 2/g" $redisPath/conf/sentinel.conf >>$logs_file 2>&1
    sed -i "/ protected-mode /a protected-mode no" $redisPath/conf/sentinel.conf >>$logs_file 2>&1
    sed -i "2a daemonize yes" $redisPath/conf/sentinel.conf >>$logs_file 2>&1
    /bin/cp -f $redisPath/conf/sentinel.conf $redisPath/conf/$sentinelPort.conf >>$logs_file 2>&1
    
    sed -i "s/^port.*/port $sentinelPort/g" $redisPath/conf/$sentinelPort.conf >>$logs_file 2>&1
    sed -i "s#^dir.*#dir $redisPath/data/$sentinelPort#g" $redisPath/conf/$sentinelPort.conf >>$logs_file 2>&1
    sed -i "/^dir/a logfile \"$redisPath/logs/sentinel_$sentinelPort.log\"" $redisPath/conf/$sentinelPort.conf >>$logs_file 2>&1
    sed -i "/^port/a pidfile \"$redisPath/pid/sentinel_$sentinelPort.pid\"" $redisPath/conf/$sentinelPort.conf >>$logs_file 2>&1
    
    /bin/cp -f $redisPath/conf/$sentinelPort.conf $redisPath/conf/$sentinelPort1.conf >>$logs_file 2>&1
    sed -i "s/$sentinelPort/$sentinelPort1/g" $redisPath/conf/$sentinelPort1.conf >>$logs_file 2>&1
    /bin/cp -f $redisPath/conf/$sentinelPort.conf $redisPath/conf/$sentinelPort2.conf >>$logs_file 2>&1
    sed -i "s/$sentinelPort/$sentinelPort2/g" $redisPath/conf/$sentinelPort2.conf >>$logs_file 2>&1
    
    #配置密码
    if [ -n "$redisPasswd" ];then
        sed -i "/ masterauth /a masterauth $redisPasswd" $redisPath/conf/$redisPort1.conf >>$logs_file 2>&1
        sed -i "/ masterauth /a masterauth $redisPasswd" $redisPath/conf/$redisPort2.conf >>$logs_file 2>&1
        sed -i "/^sentinel monitor/a sentinel auth-pass mymaster $redisPasswd" $redisPath/conf/$sentinelPort.conf >>$logs_file 2>&1
        sed -i "/^sentinel monitor/a sentinel auth-pass mymaster $redisPasswd" $redisPath/conf/$sentinelPort1.conf >>$logs_file 2>&1
        sed -i "/^sentinel monitor/a sentinel auth-pass mymaster $redisPasswd" $redisPath/conf/$sentinelPort2.conf >>$logs_file 2>&1
    fi
    
    #设置redis服务脚本
    msg="启动redis哨兵模式"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n80\n$msg\nXXX"
    echo 80
    if [ "$system_ver" != "6" ];then
        /bin/cp -f /etc/systemd/system/redis-$redisPort.service /etc/systemd/system/redis-$redisPort1.service >>$logs_file 2>&1
        /bin/cp -f /etc/systemd/system/redis-$redisPort.service /etc/systemd/system/redis-$redisPort2.service >>$logs_file 2>&1
        sed -i "s/$redisPort/$redisPort1/g" /etc/systemd/system/redis-$redisPort1.service >>$logs_file 2>&1
        sed -i "s/$redisPort/$redisPort2/g" /etc/systemd/system/redis-$redisPort2.service >>$logs_file 2>&1
        #哨兵服务脚本
        /bin/cp -f /etc/systemd/system/redis-$redisPort.service /etc/systemd/system/redis-sentinel-$sentinelPort.service >>$logs_file 2>&1
        sed -i "s/redis-server/redis-sentinel/g" /etc/systemd/system/redis-sentinel-$sentinelPort.service >>$logs_file 2>&1
        sed -i "s/$redisPort/$sentinelPort/g" /etc/systemd/system/redis-sentinel-$sentinelPort.service >>$logs_file 2>&1
        
        /bin/cp -f /etc/systemd/system/redis-sentinel-$sentinelPort.service /etc/systemd/system/redis-sentinel-$sentinelPort1.service >>$logs_file 2>&1
        sed -i "s/$sentinelPort/$sentinelPort1/g" /etc/systemd/system/redis-sentinel-$sentinelPort1.service >>$logs_file 2>&1
        /bin/cp -f /etc/systemd/system/redis-sentinel-$sentinelPort.service /etc/systemd/system/redis-sentinel-$sentinelPort2.service >>$logs_file 2>&1
        sed -i "s/$sentinelPort/$sentinelPort2/g" /etc/systemd/system/redis-sentinel-$sentinelPort2.service >>$logs_file 2>&1
        
        systemctl daemon-reload >/dev/null 2>&1
        systemctl start redis-$redisPort.service >>$logs_file 2>&1
        systemctl start redis-$redisPort1.service >>$logs_file 2>&1   
        systemctl start redis-$redisPort2.service >>$logs_file 2>&1   
        systemctl start redis-sentinel-$sentinelPort >>$logs_file 2>&1   
        systemctl start redis-sentinel-$sentinelPort1 >>$logs_file 2>&1
        systemctl start redis-sentinel-$sentinelPort2 >>$logs_file 2>&1
    else
        # 服务脚本
        /bin/cp -f /etc/init.d/redis-$redisPort /etc/init.d/redis-$redisPort1 >>$logs_file 2>&1
        sed -i "s/^PORT=.*/PORT=$redisPort1/g" /etc/init.d/redis-$redisPort1 >>$logs_file 2>&1
        /bin/cp -f /etc/init.d/redis_$redisPort /etc/init.d/redis-$redisPort2 >>$logs_file 2>&1
        sed -i "s/^PORT=.*/PORT=$redisPort2/g" /etc/init.d/redis-$redisPort2 >>$logs_file 2>&1
        
        /bin/cp -f /etc/init.d/redis-$redisPort /etc/init.d/redis-sentinel-$sentinelPort >>$logs_file 2>&1
        sed -i "s#^EXEC=.*#EXEC=\$REDIS_HOME/bin/redis-sentinel#g" /etc/init.d/redis-sentinel-$sentinelPort >>$logs_file 2>&1
        sed -i "s/^PORT=.*/PORT=$sentinelPort/g" /etc/init.d/redis-sentinel-$sentinelPort >>$logs_file 2>&1
        /bin/cp -f /etc/init.d/redis-sentinel_$sentinelPort /etc/init.d/redis-sentinel-$sentinelPort1 >>$logs_file 2>&1
        sed -i "s/^PORT=.*/PORT=$sentinelPort1/g" /etc/init.d/redis-sentinel-$sentinelPort1 >>$logs_file 2>&1
        /bin/cp -f /etc/init.d/redis-sentinel_$sentinelPort /etc/init.d/redis-sentinel_$sentinelPort2 >>$logs_file 2>&1
        sed -i "s/^PORT=.*/PORT=$sentinelPort2/g" /etc/init.d/redis-sentinel-$sentinelPort2 >>$logs_file 2>&1
        # 启动
        /etc/init.d/redis-$redisPort start >>$logs_file 2>&1    
        /etc/init.d/redis-$redisPort1 start >>$logs_file 2>&1    
        /etc/init.d/redis-$redisPort2 start >>$logs_file 2>&1    
        /etc/init.d/redis-sentinel-$sentinelPort start >>$logs_file 2>&1    
        /etc/init.d/redis-sentinel-$sentinelPort1 start >>$logs_file 2>&1    
        /etc/init.d/redis-sentinel-$sentinelPort2 start >>$logs_file 2>&1   
    fi
}

function redis_cluster() {
    msg="安装Ruby Redis"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n65\n$msg\nXXX"
    echo 65
    if [ "$die_network" == "false" ];then
        echo "修改rubygem源">>$logs_file
        gem sources --remove https://rubygems.org/ >/dev/null 2>&1
        gem sources -a https://mirrors.aliyun.com/rubygems/ >/dev/null 2>&1
        gem sources --add https://mirrors.tuna.tsinghua.edu.cn/rubygems/ >/dev/null 2>&1
        echo "安装redis.gem" >>$logs_file
        gem install redis -v 3.3.5 >>$logs_file 2>&1
        if [ "$?" != "0" ];then
          echo "使用本地的redis.gem安装" >>$logs_file
          gem install $redisHome/redis-3.3.5.gem >>$logs_file 2>&1
        fi
    else
        echo "使用本地的redis.gem安装" >>$logs_file
        gem install $redisHome/redis-3.3.5.gem >>$logs_file 2>&1
    fi
    if [ "$?" != "0" ];then
        return 1
    fi
    msg="修改配置文件"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n70\n$msg\nXXX"
    echo 70
    redisPort1=`expr $redisPort + 1`
    if [ "$PortCheck" == "Yes" ];then
        PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${redisPort1} "`
        while [[ -n $PortUsed_Pro ]]
        do
          redisPort1=`expr $redisPort1 + 1`
          PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${redisPort1} "`
        done
    fi
    redisPort2=`expr $redisPort1 + 1`
    if [ "$PortCheck" == "Yes" ];then
        PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${redisPort2} "`
        while [[ -n $PortUsed_Pro ]]
        do
          redisPort2=`expr $redisPort2 + 1`
          PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${redisPort2} "`
        done
    fi
    redisPort_s=`expr $redisPort2 + 1`
    if [ "$PortCheck" == "Yes" ];then
        PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${redisPort_s} "`
        while [[ -n $PortUsed_Pro ]]
        do
          redisPort_s=`expr $redisPort_s + 1`
          PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${redisPort_s} "`
        done
    fi
    redisPort1_s=`expr $redisPort_s + 1`
    if [ "$PortCheck" == "Yes" ];then
        PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${redisPort1_s} "`
        while [[ -n $PortUsed_Pro ]]
        do
          redisPort1_s=`expr $redisPort1_s + 1`
          PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${redisPort1_s} "`
        done
    fi
    redisPort2_s=`expr $redisPort1_s + 1`
    if [ "$PortCheck" == "Yes" ];then
        PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${redisPort2_s} "`
        while [[ -n $PortUsed_Pro ]]
        do
          redisPort2_s=`expr $redisPort2_s + 1`
          PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${redisPort2_s} "`
        done
    fi
    mkdir -p $redisPath/data/{$redisPort,$redisPort1,$redisPort2,$redisPort_s,$redisPort1_s,$redisPort2_s} >/dev/null 2>&1
    
    sed -i "/^# cluster-enabled/a cluster-enabled yes" $redisPath/conf/redis.conf >>$logs_file 2>&1
    sed -i "/^# cluster-config-file/a cluster-config-file nodes-$redisPort.conf" $redisPath/conf/redis.conf >>$logs_file 2>&1
    sed -i "/^# cluster-node-timeout/a cluster-node-timeout 15000" $redisPath/conf/redis.conf >>$logs_file 2>&1
    if [ -n "$redisPasswd" ];then
        sed -i "/ masterauth /a masterauth $redisPasswd" $redisPath/conf/redis.conf >>$logs_file 2>&1
        sed -i "s/\(@r = Redis\.new(:host.*\))/\1, :password => \"$redisPasswd\")/g" $redisPath/bin/redis-trib.rb >>$logs_file 2>&1
    fi
    /bin/cp -f $redisPath/conf/redis.conf $redisPath/conf/$redisPort.conf >>$logs_file 2>&1
    sed -i "s/^port.*/port $redisPort/g" $redisPath/conf/$redisPort.conf >>$logs_file 2>&1
    sed -i "s#^pidfile.*#pidfile $redisPath/pid/redis_$redisPort.pid#g" $redisPath/conf/$redisPort.conf >>$logs_file 2>&1
    sed -i "s#^logfile.*#logfile \"$redisPath/logs/redis_$redisPort.log\"#g" $redisPath/conf/$redisPort.conf >>$logs_file 2>&1
    sed -i "s#^dir.*#dir $redisPath/data/$redisPort#g" $redisPath/conf/$redisPort.conf >>$logs_file 2>&1
    
    /bin/cp -f $redisPath/conf/$redisPort.conf $redisPath/conf/$redisPort_s.conf >>$logs_file 2>&1
    /bin/cp -f $redisPath/conf/$redisPort.conf $redisPath/conf/$redisPort1.conf >>$logs_file 2>&1
    /bin/cp -f $redisPath/conf/$redisPort.conf $redisPath/conf/$redisPort1_s.conf >>$logs_file 2>&1
    /bin/cp -f $redisPath/conf/$redisPort.conf $redisPath/conf/$redisPort2.conf >>$logs_file 2>&1
    /bin/cp -f $redisPath/conf/$redisPort.conf $redisPath/conf/$redisPort2_s.conf >>$logs_file 2>&1
    sed -i "s/$redisPort/$redisPort_s/g" $redisPath/conf/$redisPort_s.conf >>$logs_file 2>&1
    sed -i "s/$redisPort/$redisPort1/g" $redisPath/conf/$redisPort1.conf >>$logs_file 2>&1
    sed -i "s/$redisPort/$redisPort1_s/g" $redisPath/conf/$redisPort1_s.conf >>$logs_file 2>&1
    sed -i "s/$redisPort/$redisPort2/g" $redisPath/conf/$redisPort2.conf >>$logs_file 2>&1
    sed -i "s/$redisPort/$redisPort2_s/g" $redisPath/conf/$redisPort2_s.conf >>$logs_file 2>&1
    #服务脚本
    msg="启动redis集群模式"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n80\n$msg\nXXX"
    echo 80
    if [ "$system_ver" != "6" ];then
        /bin/cp -f /etc/systemd/system/redis-$redisPort.service /etc/systemd/system/redis-$redisPort_s.service >>$logs_file 2>&1
        /bin/cp -f /etc/systemd/system/redis-$redisPort.service /etc/systemd/system/redis-$redisPort1.service >>$logs_file 2>&1
        /bin/cp -f /etc/systemd/system/redis-$redisPort.service /etc/systemd/system/redis-$redisPort1_s.service >>$logs_file 2>&1
        /bin/cp -f /etc/systemd/system/redis-$redisPort.service /etc/systemd/system/redis-$redisPort2.service >>$logs_file 2>&1
        /bin/cp -f /etc/systemd/system/redis-$redisPort.service /etc/systemd/system/redis-$redisPort2_s.service >>$logs_file 2>&1
        sed -i "s/$redisPort/$redisPort_s/g" /etc/systemd/system/redis-$redisPort_s.service >>$logs_file 2>&1
        sed -i "s/$redisPort/$redisPort1/g" /etc/systemd/system/redis-$redisPort1.service >>$logs_file 2>&1
        sed -i "s/$redisPort/$redisPort1_s/g" /etc/systemd/system/redis-$redisPort1_s.service >>$logs_file 2>&1
        sed -i "s/$redisPort/$redisPort2/g" /etc/systemd/system/redis-$redisPort2.service >>$logs_file 2>&1
        sed -i "s/$redisPort/$redisPort2_s/g" /etc/systemd/system/redis-$redisPort2_s.service >>$logs_file 2>&1
        systemctl daemon-reload >/dev/null 2>&1
        systemctl start redis-$redisPort.service >>$logs_file 2>&1
        systemctl start redis-$redisPort_s.service >>$logs_file 2>&1
        systemctl start redis-$redisPort1.service >>$logs_file 2>&1  
        systemctl start redis-$redisPort1_s.service >>$logs_file 2>&1        
        systemctl start redis-$redisPort2.service >>$logs_file 2>&1 
        systemctl start redis-$redisPort2_s.service >>$logs_file 2>&1
    else
        /bin/cp -f /etc/init.d/redis-$redisPort /etc/init.d/redis-$redisPort_s >>$logs_file 2>&1
        /bin/cp -f /etc/init.d/redis-$redisPort /etc/init.d/redis-$redisPort1 >>$logs_file 2>&1
        /bin/cp -f /etc/init.d/redis-$redisPort /etc/init.d/redis-$redisPort1_s >>$logs_file 2>&1
        /bin/cp -f /etc/init.d/redis-$redisPort /etc/init.d/redis-$redisPort2 >>$logs_file 2>&1
        /bin/cp -f /etc/init.d/redis-$redisPort /etc/init.d/redis-$redisPort2_s >>$logs_file 2>&1
        sed -i "s/^PORT=.*/PORT=$redisPort_s/g" /etc/init.d/redis-$redisPort_s >>$logs_file 2>&1
        sed -i "s/^PORT=.*/PORT=$redisPort1/g" /etc/init.d/redis-$redisPort1 >>$logs_file 2>&1
        sed -i "s/^PORT=.*/PORT=$redisPort1_s/g" /etc/init.d/redis-$redisPort1_s >>$logs_file 2>&1
        sed -i "s/^PORT=.*/PORT=$redisPort2/g" /etc/init.d/redis-$redisPort2 >>$logs_file 2>&1
        sed -i "s/^PORT=.*/PORT=$redisPort2_s/g" /etc/init.d/redis-$redisPort2_s >>$logs_file 2>&1
        /etc/init.d/redis-$redisPort start >>$logs_file 2>&1  
        /etc/init.d/redis-$redisPort_s start >>$logs_file 2>&1        
        /etc/init.d/redis-$redisPort1 start >>$logs_file 2>&1 
        /etc/init.d/redis-$redisPort1_s start >>$logs_file 2>&1        
        /etc/init.d/redis-$redisPort2 start >>$logs_file 2>&1 
        /etc/init.d/redis-$redisPort2_s start >>$logs_file 2>&1        
    fi
    pidnum=`pidof redis-server|wc|awk '{print $2}'`
    if [ "$pidnum" == "6" ];then
        msg="创建redis集群"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n85\n$msg\nXXX"
        echo 85
        echo -e "yes\n"|$redisPath/bin/redis-trib.rb create --replicas 1 $ipaddr:$redisPort $ipaddr:$redisPort1 $ipaddr:$redisPort2 $ipaddr:$redisPort_s $ipaddr:$redisPort1_s $ipaddr:$redisPort2_s >>$logs_file 2>&1
    else
        msg="redis服务启动异常，无法创建redis集群"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n85\n$msg\nXXX"
        echo 85
    fi
}

function install_check() {
    if [ ! -f /tmp/redis-comp.log ];then
        errInfo=`cat /tmp/.redis.err.info`
        rm -rf /tmp/.redis.err.info
        msg="Redis安装失败！！！"
        whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "$msg\n\n$errInfo" 10 60
        sleep 0.15
        return 1
    fi

    
    if [ "$redis_run_type" == "1" ];then
        redisPid=`pidof redis-server`
        if [ -n "$redisPid" -a "$system_ver" = "6" ];then
            msg="Redis安装成功，你可以使用service redis start/stop/status/restart命令管理redis服务"
            echo $msg>>$logs_file
            whiptail --backtitle "Author: Elvis" --title "信息" --msgbox "$msg" 10 60
        elif [ -n "$redisPid" -a "$system_ver" != "6" ];then
            msg="Redis安装成功，你可以使用systemctl start/stop/status/restart redis命令管理redis服务"
            echo $msg>>$logs_file
            whiptail --backtitle "Author: Elvis" --title "信息" --msgbox "$msg" 10 60
        else
            msg="Redis安装失败，请检查日志文件及redis配置文件！！！"
            echo $msg>>$logs_file
            whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "$msg" 10 60
            return 1
        fi
    elif [ "$redis_run_type" == "2" ];then
        redisPid=`pidof redis-server|wc|awk '{print $2}'`
        sentinelPid=`pidof redis-sentinel|wc|awk '{print $2}'`
        if [ "$redisPid" = "3" -a "$sentinelPid" = "3" ];then
           msg="Redis哨兵模式安装成功"
           echo $msg>>$logs_file
           whiptail --backtitle "Author: Elvis" --title "信息" --msgbox "$msg" 10 60
        else
           msg="Redis哨兵模式安装失败"
           echo $msg>>$logs_file
           whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "$msg" 10 60
           return 1
        fi
    else
        redisPid=`pidof redis-server|wc|awk '{print $2}'`
        if [ "$redisPid" = "6" ];then
            msg="Redis集群模式安装成功"
            echo $msg>>$logs_file
            whiptail --backtitle "Author: Elvis" --title "信息" --msgbox "$msg" 10 60
        else
            msg="Redis集群模式安装失败"
            echo $msg>>$logs_file
            whiptail --backtitle "Author: Elvis" --title "信息" --msgbox "$msg" 10 60
            return 1
        fi
    fi
}


function install_redis() {
    echo "安装Redis" >>$logs_file
    rm -rf /tmp/redis-comp.log
    redisPath=$(whiptail --backtitle "Author: Elvis" --title "redis安装位置" --inputbox \
        "输入redis安装位置，默认位置/usr/local/redis，切记不要输入一个系统目录" 12 60 "/usr/local/redis" 3>&1 1>&2 2>&3)
    if [ "$?" != "0" ];then
        sleep 0.20
        return 1
    fi
    redisPath=`echo $redisPath`
    redisPath=${redisPath%/}

    redisPid=`pidof redis-server`
    sentinelPid=`pidof redis-sentinel`
    if [ -n "$redisPid" -o -n "$sentinelPid" ];then
        whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "系统中已经有Redis在运行，请先停止或卸载！！！" 10 60
        sleep 0.15
        return 1
    else
        if [ -d "$redisPath" ];then
           whiptail --backtitle "Author: Elvis" --title "警告" --msgbox "安装目录$redisPath已经存在，继续安装会删除此目录，建议继续安装前做好备份！" 10 60
        fi
    fi
    redisHome="$workHome/packages/redis"
    cd $redisHome
    redisVer=$(whiptail --backtitle "Author: Elvis" --title "Redis安装配置" --inputbox \
        "输入需要安装的Redis版本，如果本地不存在，则需要联网下载" 12 60 "3.2.13" 3>&1 1>&2 2>&3)
    if [ "$?" != "0" ];then
        sleep 0.20
        return 1
    fi
    redisVer=`echo $redisVer`
    redis_run_type=$(whiptail --backtitle "Author: Elvis" --title "运行模式" --radiolist \
    "请选择Redis模式？" 12 60 3 \
    "1" "单机模式                                " ON \
    "2" "哨兵模式" OFF \
    "3" "集群模式" OFF 3>&1 1>&2 2>&3)
    if [ "$?" != "0" ];then
        return 1
    else
        if [ "$redis_run_type" == "1" ];then
          runMode="单机模式"
        elif [ "$redis_run_type" == "2" ];then
          runMode="哨兵模式"
        else
          runMode="集群模式"
        fi
    fi
    
    if [ "$redis_run_type" == "3" ];then
        main_redisVer=`echo $redisVer|awk -F"." '{print $1}'`
        if [[ $main_redisVer -lt 3 ]];then
           whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "Redis集群模式必须使用redis 3以上版本！！！" 10 60
           sleep 0.15
           return 1
        fi
    fi
    
    command -v ss >/dev/null 2>&1
    if [ "$?" == "0" ];then
        listen_cmd="ss -lnpt"
    else
        command -v netstat >/dev/null 2>&1
        if [ "$?" == "0" ];then
          listen_cmd="netstat -tnlp"
        fi
    fi
    [[ $listen_cmd ]] && PortCheck=Yes || PortCheck=No
    PortUsed=
    PortUsed_Pro=
    redisPort=
    while [[ ! "$redisPort" =~ ^[1-9][0-9]+$ || "$PortUsed" == "Yes" ]]
    do
        redisPort=$(whiptail --backtitle "Author: Elvis" --title "Redis端口设置" --inputbox --nocancel \
            "输入Redis初始端口，如初始端口为6379，则哨兵模式的redis端口为6379-6381，哨兵端口为26379-26381" 12 60 "6379" 3>&1 1>&2 2>&3)
        if [[ ! "$redisPort" =~ ^[1-9][0-9]+$ ]];then
            whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "redis端口只能为两位及以上的且不以0开头的数字组成，请重新输入！" 10 60
            sleep 0.5
        else
            if [ "$PortCheck" == "Yes" ];then
                PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${redisPort} "`
                if [ -n "$PortUsed_Pro" ];then
                    PortUsed=Yes
                    whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "Redis端口已被使用，请重新输入！" 8 60
                else
                    PortUsed=No
                fi
            else
                PortUsed=No
            fi
        fi
        done
    redisPort=`echo $redisPort`
    
    redisPasswd=$(whiptail --backtitle "Author: Elvis" --title "Redis密码设置" --inputbox --nocancel \
        "输入Redis密码，留空则不使用密码" 10 60 3>&1 1>&2 2>&3)
    if [ "$?" != "0" ];then
        sleep 0.20
        return 1
    fi
    redisPasswd=`echo $redisPasswd`
    if [ -z "$redisVer" ];then
        redisVer="3.2.13"
    fi
    
    if [ -z "$redisPath" ];then
        redisPath=/usr/local/redis
    fi
    
    if [ ! -f $redisHome/redis-$redisVer.tar.gz -a "$die_network" == "ture" ];then
        whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "系统未联网，且本地不存在需要安装的Reids版本！！！" 10 70
        sleep 0.15
        return 1
    fi
    
    whiptail --backtitle "Author: Elvis" --title "Redis安装信息汇总" --yesno \
    "\nRedis版本：$redisVer\nRdis安装位置：$redisPath\nRedis端口：$redisPort\nRedis运行模式：$runMode\nRedis密码：$redisPasswd\n\nYes以继续安装，No退出Redis安装" 15 70
    if [ "$?" != "0" ];then
        sleep 0.10
        return 1
    fi
    
    {
    echo 1
    if [ "$die_network" == "true" ];then
        ping -c 2 -w 5 180.76.76.76 >/dev/null 2>&1
        if [ "$?" != "0" ];then
           die_network="true"
        else
           die_network="false"
        fi
    else
        sleep 1
    fi
    if [[ ! -f $redisHome/redis-$redisVer.tar.gz && "$die_network" == "false" ]];then
        msg="下载Redis安装包"
        echo $msg >>$logs_file
        echo -e "XXX\n10\n$msg\nXXX"
        echo 10
        echo "首先使用curl下载，curl失败后尝试使用wget下载" >>$logs_file
        echo -e "XXX\n15\n下载Redis安装包中......\nXXX"
        echo 15
        curl --fail -O http://download.redis.io/releases/redis-$redisVer.tar.gz >>$logs_file 2>&1
        if [ "$?" != "0" ];then
            sleep 1
            rm -rf $redisHome/redis-$redisVer.tar.gz
            echo "">>$logs_file
            wget -t5 http://download.redis.io/releases/redis-$redisVer.tar.gz >>$logs_file 2>&1
        fi
                
        if [ "$?" != "0" ];then
           msg="下载Redis失败，请检查网络或者不存在此版本的Redis！！！"
           echo $msg >>$logs_file
           echo -e "XXX\n15\n$msg\nXXX"
           echo 15
           echo "$msg" >/tmp/.redis.err.info
           sleep 3
           return 1
        fi
    elif [[ ! -f $redisHome/redis-$redisVer.tar.gz && "$die_network" == "true" ]];then
        msg="你的主机未联网，请提前手动下载安装包，使用离线安装！！！"
        echo $msg >>$logs_file
        echo -e "XXX\n15\n$msg\nXXX"
        echo 15
        echo "$msg" >/tmp/.redis.err.info
        sleep 3
        return 1
    fi
    
    
    msg="安装所需基础RPM包"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n20\n$msg\nXXX"
    echo 20
    if [ "$redis_run_type" == "3" ];then
        yum -y install gcc glibc gcc-c++ tcl jemalloc ruby rubygems >>$logs_file 2>&1
    else
        yum -y install gcc glibc gcc-c++ tcl jemalloc>>$logs_file 2>&1
    fi
    if [ "$?" != "0" ];then
        msg="基础依赖软件失败，退出安装！！！"
        echo $msg >>$logs_file
        echo -e "XXX\n25\n$msg\nXXX"
        echo 25
        echo "$msg" >/tmp/.redis.err.info
        sleep 3
        return 1
    fi
    find $redisHome/* -type d|xargs rm -rf
    echo "注意：以下编译安装过程，仅输出错误及警告信息，编译日志存放在/tmp/redis-comp.log" >>$logs_file
    echo >/tmp/redis-comp.log
  
    msg="安装Redis"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n30\n$msg\nXXX"
    echo 30
    echo "" >>/tmp/redis-comp.log
    echo "$msg" >>/tmp/redis-comp.log
    cd $redisHome
    tar zxf redis-$redisVer.tar.gz >/dev/null 2>>$logs_file
    
    cd redis-$redisVer && echo -e "\n\n使用make编译">>/tmp/redis-comp.log && echo -e "XXX\n40\n编译Redis中......\nXXX"&&echo 40 && \
    make -j8 >>/tmp/redis-comp.log 2>>$logs_file && echo -e "\n\nmake install安装Redis">>/tmp/redis-comp.log && echo -e "XXX\n50\n安装Redis中......\nXXX" \
    &&echo 50&& make install >>/tmp/redis-comp.log 2>>$logs_file
    if [ "$?" != "0" ];then
        msg="安装Redis失败，退出安装！！！"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n55\n$msg\nXXX"
        echo 55
        echo "$msg" >/tmp/.redis.err.info
        sleep 3
        return 1
    fi
    echo " ">>$logs_file
    echo " ">>/tmp/redis-comp.log
    msg="复制Redis到安装目录"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n60\n$msg\nXXX"
    echo 60
    rm -rf $redisPath
    mkdir -p $redisPath/{bin,conf,logs,pid,data}
    cd $redisHome/redis-$redisVer
    /bin/cp -f redis.conf sentinel.conf $redisPath/conf/ >>$logs_file 2>&1
    cd src/
    /bin/cp -f redis-cli redis-sentinel redis-trib.rb redis-server redis-benchmark redis-check-aof $redisPath/bin >>$logs_file 2>&1
    for i in `ls -F |grep "\*"`
    do
        /bin/cp -f $i $redisPath/bin/ >/dev/null 2>&1
    done
    chmod +x $redisPath/bin/*
    sed -i "s/^bind.*/bind $ipaddr/g" $redisPath/conf/redis.conf >>$logs_file 2>&1
    sed -i "s/^daemonize.*/daemonize yes/g" $redisPath/conf/redis.conf >>$logs_file 2>&1
    if [ -n "$redisPasswd" ];then
        sed -i "/[# ]*requirepass .*/a requirepass $redisPasswd" $redisPath/conf/redis.conf  >>$logs_file 2>&1
    fi
    if [ "$system_ver" == "6" ];then
        cat >/etc/init.d/redis-$redisPort<<'EOF'
#!/bin/bash
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig:   - 85 15
# description:  Redis is a persistent key-value database
# processname: redis-server
  
# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network
  
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

PORT=6379
REDIS_HOME=/usr/local/redis
EXEC=$REDIS_HOME/bin/redis-server
PROG=$(basename $EXEC)
if [ "$PROG" == "redis-server" ];then
    PREFIX=redis
else
    PREFIX=sentinel
fi
CONF_FILE=$REDIS_HOME/conf/$PORT.conf
PID_FILE=$REDIS_HOME/pid/$PREFIX_${PORT}.pid
lockfile=/var/lock/subsys/$PREFIX_$PORT


start() {
    [ -x $EXEC ] || exit 5
    [ -f $CONF_FILE ] || exit 6
    echo -n $"Starting $PROG: "
    daemon $EXEC $CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
  
stop() {
    echo -n $"Stopping $PROG: "
    killproc -p $PID_FILE $PROG
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
  
restart() {
    stop
    start
}
  
reload() {
    echo -n $"Reloading $PROG: "
    killproc -p $PID_FILE $EXEC -HUP
    RETVAL=$?
    echo
}

  
rh_status() {
    status -p $PID_FILE $PROG
}
  
rh_status_q() {
    rh_status >/dev/null 2>&1
}
  
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    status)
        rh_status
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|reload}"
        exit 2
esac
EOF
        sed -i "s/^REDIS_HOME=.*/REDIS_HOME=$redisPath/g" /etc/init.d/redis-$redisPort >>$logs_file 2>&1
        sed -i "s/^PORT=.*/PORT=$redisPort/g" /etc/init.d/redis-$redisPort >>$logs_file 2>&1
        chmod +x /etc/init.d/redis-$redisPort
    else
        cat >/etc/systemd/system/redis-$redisPort.service<<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=$redisPath/bin/redis-server $redisPath/conf/$redisPort.conf --supervised systemd
ExecStop=/usr/libexec/redis-shutdown redis-server $redisPath/conf/$redisPort.conf
Type=notify

[Install]
WantedBy=multi-user.target
EOF
        cat>/usr/libexec/redis-shutdown<<'EOF'
#!/bin/bash
#
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x && set -x

REDIS_CLI=/usr/bin/redis-cli

# Retrieve service name
SERVICE_NAME="$1"
if [ -z "$SERVICE_NAME" ]; then
   SERVICE_NAME=redis-server
fi

# Get the proper config file based on service name
CONFIG_FILE="$2"

# Use awk to retrieve host, port from config file
HOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`
PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`
PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`
SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1`

# Just in case, use default host, port
HOST=${HOST:-127.0.0.1}
if [ "$SERVICE_NAME" = redis-server ]; then
    PORT=${PORT:-6379}
else
    PORT=${PORT:-26739}
fi

# Setup additional parameters
# e.g password-protected redis instances
[ -z "$PASS"  ] || ADDITIONAL_PARAMS="-a $PASS"

# shutdown the service properly
if [ -e "$SOCK" ] ; then
    $REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown
else
    $REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown
fi
EOF
      sed -i "s#^REDIS_CLI=.*#REDIS_CLI=$redisPath/bin/redis-cli#g" /usr/libexec/redis-shutdown >>$logs_file 2>&1
    fi
    
    if [ "$redis_run_type" == "1" ];then
        redis_alone
    elif [ "$redis_run_type" == "2" ];then
        redis_sentinel
    else
        redis_cluster     
    fi
    echo 95
    sleep 0.30
    echo 100
    sleep 0.30
        
    } | whiptail --backtitle "Author: Elvis" --title "安装Redis" --gauge " " 6 60 0
    install_check
}




